package com.bw.visitor.view

import android.app.DatePickerDialog
import android.app.Dialog
import android.app.TimePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.visitor.R
import com.bw.visitor.databinding.ActivityAddVisitorBinding
import com.bw.visitor.entity.VisitorEntity
import com.bw.visitor.net.VisitorViewModel
import java.util.Calendar

/**
 * 智慧园区-访客管理
 * 新镇来访
 * 刘哲甫
 * 20250512
 */
@Route(path = RoutePath.PATH_ADD_VISITOR)
class AddVisitorActivity : BaseMvvmActivity<ActivityAddVisitorBinding, VisitorViewModel>() {

    var visitorEntity: VisitorEntity = VisitorEntity()
    var isRequestParking : Boolean = false

    override fun initView() {
        //返回
        binding.back.setOnClickListener {
            finish()
        }

        //日期选择
        binding.data.setOnClickListener {
            // 获取当前日期
            val calendar = Calendar.getInstance()
            val year = calendar.get(Calendar.YEAR)
            val month = calendar.get(Calendar.MONDAY)
            val day = calendar.get(Calendar.DAY_OF_MONTH)

            // 创建日期选择对话框
            val dataPickerDialog = DatePickerDialog(
                this,
                { _, selectedYear, selectedMonth, selectedDay ->
                    // 处理选择的日期
                    val selectedDate = "$selectedYear-${selectedMonth + 1}-$selectedDay"
                    Toast.makeText(this, "选择的日期是: $selectedDate", Toast.LENGTH_SHORT).show()
                    binding.appointmentTimeData.setText("$selectedDate")
                },
                year, month, day
            )
            //显示日期选择器
            dataPickerDialog.show()
        }

        //开始时间 K_time
        binding.timeKai.setOnClickListener {
            //获取当前时间
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            val minute = calendar.get(Calendar.MINUTE)
            val second = calendar.get(Calendar.SECOND)

            //创建时间选择对话框
            val timePickerDialog = TimePickerDialog(
                this,
                { _, Hour, Minute ->
                    val selectedTime = String.format("%02d:%02d", Hour, Minute)
                    Toast.makeText(this, "选择的时间是: $selectedTime", Toast.LENGTH_SHORT).show()
                    binding.KTime.setText("$selectedTime")
                },
                hour, minute, true
            )
            //显示时间选择对话框
            timePickerDialog.show()
        }

        //结束时间 K_time
        binding.timeJie.setOnClickListener {
            //获取当前时间
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            val minute = calendar.get(Calendar.MINUTE)

            //创建时间选择对话框
            val timePickerDialog = TimePickerDialog(
                this,
                { _, Hour, Minute ->
                    val selectedTime = String.format("%02d:%02d", Hour, Minute)
                    Toast.makeText(this, "选择的时间是: $selectedTime", Toast.LENGTH_SHORT).show()
                    binding.JTime.setText("$selectedTime")
                },
                hour, minute, true
            )
            //显示时间选择对话框
            timePickerDialog.show()
        }


    }

    override fun initData() {

        binding.mySwitch.setOnCheckedChangeListener { compoundButton, b ->
            isRequestParking = b
        }

        binding.visitorSave.setOnClickListener {
            visitorEntity.visitorName = binding.etVisitorName.text.toString()
            visitorEntity.appointmentTime =
                "${binding.appointmentTimeData.text} ${binding.KTime.text}"
            visitorEntity.phoneNumber = binding.etPhoneNumber.text.toString()
            visitorEntity.purpose = binding.etPurpose.text.toString()
            visitorEntity.employeeId = binding.etEmployeeId.id
            visitorEntity.idNumber = binding.etIdNumber.text.toString()
            visitorEntity.requestParking = isRequestParking


            if (visitorEntity.visitorName == "" ||
                visitorEntity.appointmentTime == "" ||
                visitorEntity.phoneNumber == "" ||
                visitorEntity.purpose == "" ||
                visitorEntity.idNumber == ""
            ) {
                ToastUtils.showLong("请先输入内容")
                return@setOnClickListener
            }

            vm.addVisitor(visitorEntity)

        }

        vm.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
        }

        vm.onFailed.observe(this) {
            ToastUtils.showLong("添加失败")
        }

        vm.visitorSuccess.observe(this) {
            ToastUtils.showLong(it.size)
        }
    }


}
